package com.cskaoyan.service;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.MarketIssueExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Date;
import java.util.List;

public class IssueServiceImpl implements IssueService {
    SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();

    @Override
    public List<MarketIssue> query(String question, Integer page, Integer limit, String sort, String order) {

        MarketIssueMapper issueMapper = sqlSessionFactory.openSession().getMapper(MarketIssueMapper.class);

        MarketIssueExample example = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = example.createCriteria();

        if (!StringUtil.isEmpty(question)) {
            criteria.andQuestionLike("%" + question + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return issueMapper.selectByExample(example);
    }

    @Override
    public BaseRespVo create(MarketIssue issue) {
        // TransactionUtil.startTransaction();

        BaseRespVo error = validate(issue);
        if (error != null) {
            return error;
        }

        // this.add(issue);
        // TransactionUtil.commit();

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper mapper = sqlSession.getMapper(MarketIssueMapper.class);
        mapper.insertSelective(issue);
        sqlSession.commit();
        sqlSession.close();
        return BaseRespVo.ok(issue);
    }

    @Override
    public int add(MarketIssue issue) {
        Date now = new Date();
        issue.setUpdateTime(now);
        issue.setAddTime(now);
        MarketIssueMapper issueMapper = TransactionUtil.getMapper(MarketIssueMapper.class);
        return issueMapper.insertSelective(issue);
    }

    @Override
    public int deleteById(Integer issueId) {

        MarketIssueMapper issueMapper = TransactionUtil.getMapper(MarketIssueMapper.class);

        int i = issueMapper.deleteByPrimaryKey(issueId);

        return i;
    }

    @Override
    public BaseRespVo remove(MarketIssue issue) {
        TransactionUtil.startTransaction();
        Integer id = issue.getId();
        if (id == null) {
            return BaseRespVo.badArgument();
        }

//        this.deleteById(id);
        MarketIssueMapper issueMapper = TransactionUtil.getMapper(MarketIssueMapper.class);
        int delete = issueMapper.deleteByPrimaryKey(id);
        if (delete == 0) {
            return BaseRespVo.fail(555, "删除失败");
        }
        TransactionUtil.commit();
        return BaseRespVo.ok(issue);
    }

    @Override
    public int update2(MarketIssue marketIssue) {
        Date now = new Date();
        marketIssue.setUpdateTime(now);
        MarketIssueMapper marketIssueMapper = TransactionUtil.getMapper(MarketIssueMapper.class);
        return marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
    }

    @Override
    public BaseRespVo update(MarketIssue issue) {
        TransactionUtil.startTransaction();
        BaseRespVo error = validate(issue);
        if (error != null) {
            return error;
        }

//        this.update2(issue);
        Integer issueId = issue.getId();
        if (issueId == null) {
            return BaseRespVo.fail(654, "Id为空");
        }

        Date now = new Date();
        MarketIssueMapper issueMapper = TransactionUtil.getMapper(MarketIssueMapper.class);
        int update = issueMapper.updateByPrimaryKeySelective(issue);
        if (update == 0) {
            return BaseRespVo.fail(852, "更新数据失败");
        }
        TransactionUtil.commit();
        return BaseRespVo.ok();
    }


    private BaseRespVo validate(MarketIssue issue) {
        String question = issue.getQuestion();
        String answer = issue.getAnswer();
        if (StringUtil.isEmpty(question) || StringUtil.isEmpty(answer)) {
            return BaseRespVo.badArgument();
        }
        if (StringUtil.isEmpty(answer)) {
            return BaseRespVo.badArgument();
        }

        return null;
    }

    private MarketIssue findById(Integer issueId) {

        MarketIssueMapper issueMapper = sqlSessionFactory.openSession().getMapper(MarketIssueMapper.class);
        return issueMapper.selectByPrimaryKey(issueId);
    }
}
